package com.cn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.api.ApiCode;
import com.cn.api.ApiResult;
import com.cn.mapper.ShopCateMapper;
import com.cn.mapper.ShopCtcMapper;
import com.cn.pojo.shop.ShopCate;
import com.cn.pojo.shop.ShopCtc;
import com.cn.pojo.vo.ShopCateCtcVo;
import com.cn.service.ShopCateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 28631
* @description 针对表【shop_cate(分类)】的数据库操作Service实现
* @createDate 2024-02-29 15:12:20
*/
@Service
public class ShopCateServiceImpl extends ServiceImpl<ShopCateMapper, ShopCate>
    implements ShopCateService {

    @Autowired
    private ShopCateMapper shopCateMapper;

    @Autowired
    private ShopCtcMapper shopCtcMapper;

    /**
     * 查询分类数据信息
     * @return
     */
    @Override
    public ApiResult queryCate() {
        List<ShopCateCtcVo> list = shopCateMapper.queryCate();
        return ApiResult.success(list);
    }

    /**
     *
     * @param shopCate 分类
     * @return
     */
    @Override
    public ApiResult updateCate(ShopCate shopCate) {
        //修改分类信息
        int i = shopCateMapper.updateById(shopCate);
        if (i>0) {
            return ApiResult.success(ApiCode.SUCCESS);
        }
        return ApiResult.error(ApiCode.SYSTEM_ERROR.getCode(), ApiCode.SYSTEM_ERROR.getMsg());
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public ApiResult deleteCate(Integer id) {
        //查询是否存在下级id
        ShopCate shopCate = shopCateMapper.selectById(id);
        LambdaQueryWrapper<ShopCtc> shopCtcLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shopCtcLambdaQueryWrapper.eq(ShopCtc::getCateId,shopCate.getId());
        List<ShopCtc> shopCtc = shopCtcMapper.selectList(shopCtcLambdaQueryWrapper);
        if (shopCtc != null && shopCtc.size()>0) {
            return ApiResult.error(ApiCode.NOT_USER.getCode(), ApiCode.NOT_USER.getMsg());
        }
        //删除分类信息
        int i = shopCateMapper.deleteById(shopCate);
        return ApiResult.success(ApiCode.SUCCESS);
    }

    /**
     * 增加分类信息
     * @param shopCate
     * @return
     */
    @Override
    public ApiResult addCate(ShopCate shopCate) {
        //增加分类
        int insert = shopCateMapper.insert(shopCate);
        if (insert>0) {
            return ApiResult.success(ApiCode.SUCCESS);
        }
        return ApiResult.error(ApiCode.NOT_USER.getCode(),ApiCode.NOT_USER.getMsg());
    }
}




